package com.mycompany.app.common.extjs;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.mycompany.app.common.extjs.json.DateJsonValueProcessor;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ExtJSBaseAction extends ActionSupport {

    private static JsonConfig jsonConfig;
    static {
        jsonConfig = new JsonConfig();
        jsonConfig.setIgnoreDefaultExcludes(false);
        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        jsonConfig.registerJsonValueProcessor(Date.class,
                new DateJsonValueProcessor());
    }

    protected InputStream inputStream;

    public InputStream getInputStream() {

        return inputStream;
    }

    @SuppressWarnings( { "unused", "unchecked" })
    protected String objectList2json(List list) {

        return JSONArray.fromObject(list, jsonConfig).toString();
    }

    protected void printObject(Object o) {

        System.out.println(ToStringBuilder.reflectionToString(o));
    }
    
    protected void ajaxEcho(String msg)
    {
        inputStream = new ByteArrayInputStream(msg.getBytes());
    }

}
